from gui import gui

class application_window_fake :
    pass

class button_fake :
    _deleted_count = 0
    def __init__ ( self , master , text , command , anchor , borderwidth ) :
        self . _args = [ master , text , command , anchor , borderwidth ]
    def __del__ ( self ) :
        button_fake . _deleted_count += 1

class entry_fake :
    _deleted_count = 0
    def __init__ ( self , master , borderwidth , textvariable ) :
        self . _args = [ master , borderwidth , textvariable ]
    def __del__ ( self ) :
        entry_fake . _deleted_count += 1
    def icursor ( self , index ) :
        pass

class frame_fake :
    _deleted_count = 0
    def __init__ ( self , master ) :
        self . _args = [ master ]
    def __del__ ( self ) :
        frame_fake . _deleted_count += 1

class image_fake :
    _deleted_count = 0
    def __init__ ( self , master , image ) :
        self . _args = [ master , image ]
    def __del__ ( self ) :
        image_fake . _deleted_count += 1

class label_fake :
    _deleted_count = 0
    def __init__ ( self , master , text , borderwidth , relief , anchor , textvariable ) :
        self . _args = [ master , text , borderwidth , relief , anchor , textvariable ]
    def __del__ ( self ) :
        label_fake . _deleted_count += 1

class text_fake :
    _deleted_count = 0
    def __init__ ( self , master ) :
        self . _args = [ master ]
    def __del__ ( self ) :
        text_fake . _deleted_count += 1

class photo_image_fake :
    pass

class string_var_fake :
    _deleted_count = 0
    def __init__ ( self ) :
        self . _text = None
    def __del__ ( self ) :
        string_var_fake . _deleted_count += 1
    def set ( self , text ) :
        self . _text = text

class tkinter_fake :
    def __init__ ( self ) :
        self . _application_windows = [ ]
        self . _button_collection = [ ]
        self . _entry_collection = [ ]
        self . _frame_collection = [ ]
        self . _image_collection = [ ]
        self . _label_collection = [ ]
        self . _text_collection = [ ]
        self . _string_vars = [ ]
        self . END = None
    def Button ( self , master , _fake_arg = None , text = None , command = None , anchor = None , borderwidth = None ) :
        button = button_fake ( master , text , command , anchor , borderwidth )
        self . _button_collection += [ button ]
        return button
    def Entry ( self , master , _fake_arg = None , borderwidth = None , textvariable = None ) :
        entry = entry_fake ( master , borderwidth , textvariable )
        self . _entry_collection += [ entry ]
        return entry
    def Frame ( self , master ) :
        frame = frame_fake ( master )
        self . _frame_collection += [ frame ]
        return frame
    def Label ( self , master , _fake_arg = None , text = None , borderwidth = None , relief = None , anchor = None , textvariable = None , image = None ) :
        if image != None :
            image_widget = image_fake ( master , image )
            self . _image_collection += [ image_widget ]
            return image_widget
        else :
            label = label_fake ( master , text , borderwidth , relief , anchor , textvariable )
            self . _label_collection += [ label ]
            return label
    def PhotoImage ( self , _fake_arg = None , data = None ) :
        return photo_image_fake ( )
    def Text ( self , master ) :
        text = text_fake ( master )
        self . _text_collection += [ text ]
        return text
    def StringVar ( self ) :
        string_var = string_var_fake ( )
        self . _string_vars += [ string_var ]
        return string_var
    def Tk ( self ) :
        window = application_window_fake ( )
        self . _application_windows += [ window ]
        return window

class mediator_fake :
    def __init__ ( self ) :
        self . _gui_create_application_window_reply = None
        self . _gui_create_button_reply = None
        self . _gui_create_entry_reply = None
        self . _gui_create_frame_reply = None
        self . _gui_create_image_reply = None
        self . _gui_create_label_reply = None
        self . _gui_create_text_reply = None
    def gui_create_application_window_reply ( self , window ) :
        self . _gui_create_application_window_reply = window
    def gui_create_button_reply ( self , button ) :
        self . _gui_create_button_reply = button
    def gui_create_entry_reply ( self , entry ) :
        self . _gui_create_entry_reply = entry
    def gui_create_frame_reply ( self , frame ) :
        self . _gui_create_frame_reply = frame
    def gui_create_image_reply ( self , image ) :
        self . _gui_create_image_reply = image
    def gui_create_label_reply ( self , label ) :
        self . _gui_create_label_reply = label
    def gui_create_text_reply ( self , text ) :
        self . _gui_create_text_reply = text

class gui_create_widget_test_assistant :
    def __init__ ( self , test ) :
        self . _test = test
        self . _tkinter = tkinter_fake ( )
        self . _mediator = mediator_fake ( )
        self . _gui = gui ( mediator = self . _mediator , tkinter = self . _tkinter , tkfont = None , scheduler = None )
        button_fake . _deleted_count = 0
        entry_fake . _deleted_count = 0
        frame_fake . _deleted_count = 0
        image_fake . _deleted_count = 0
        label_fake . _deleted_count = 0
        text_fake . _deleted_count = 0
        string_var_fake . _deleted_count = 0

    #
    # arbitrary element creation test helpers
    #
    
    def check_gui_create_arbitrary_element ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        self . _test . assume_equal_len ( getattr ( self . _tkinter , '_%s_collection' % element_name ) , 1 )
    def check_gui_create_arbitrary_element_args ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        self . _test . assume_equal ( getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] . _args , 
            [ self . _tkinter . _application_windows [ 0 ] 
            ] + list ( args ) )
    def check_gui_create_arbitrary_element_persistance ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        setattr ( self . _tkinter , '_%s_collection' % element_name , [ ] )
        self . _test . assume_equal ( eval ( '%s_fake' % element_name ) . _deleted_count , 0 )
    def check_gui_create_arbitrary_element_destruction ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        setattr ( self . _tkinter , '_%s_collection' % element_name , [ ] )
        setattr ( self . _mediator , '_gui_create_%s_reply' % element_name , None )
        del element
        self . _test . assume_equal ( eval ( '%s_fake' % element_name ) . _deleted_count , 1 )
    def check_gui_create_arbitrary_element_encapsulation ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        self . _test . assume_not_equal ( getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] , element )
    def check_gui_create_arbitrary_element_child ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( element , * args )
        child = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        self . _test . assume_equal ( getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 1 ] . _args [ 0 ] , 
            getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] 
        )

    #
    # arbitrary element creation with string var test helpers
    #
            
    def check_gui_create_arbitrary_element_string_var ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        self . _test . assume_equal_len ( self . _tkinter . _string_vars , 1 )
    def check_gui_create_arbitrary_element_args_with_string_var ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        self . _test . assume_equal ( getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] . _args
            , [ self . _tkinter . _application_windows [ 0 ] ]
            + list ( args )
            + [ self . _tkinter . _string_vars [ 0 ] ]
            )
    def check_gui_create_arbitrary_element_string_var_persistance ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] . _args = None
        setattr ( self . _tkinter , '_%s_collection' % element_name , [ ] )
        self . _tkinter . _string_vars = [ ]
        self . _test . assume_equal ( string_var_fake . _deleted_count , 0 )
    def check_gui_create_arbitrary_element_string_var_destruction ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        getattr ( self . _tkinter , '_%s_collection' % element_name ) [ 0 ] . _args = None
        setattr ( self . _tkinter , '_%s_collection' % element_name , [ ] )
        setattr ( self . _mediator , '_gui_create_%s_reply' % element_name , None )
        self . _tkinter . _string_vars = [ ]
        del element
        self . _test . assume_equal ( string_var_fake . _deleted_count , 1 )
    def check_gui_set_arbitrary_element_text ( self , element_name , * args ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_create_%s_request' % element_name ) ( window , * args )
        element = getattr ( self . _mediator , '_gui_create_%s_reply' % element_name )
        getattr ( self . _gui , 'gui_set_%s_text' % element_name ) ( element , 'my text' )
        self . _test . assume_equal ( self . _tkinter . _string_vars [ 0 ] . _text , 'my text' )
